<?php
   $auth = Auth::getAuth('current_user');
   $userCompany = new userQueries();
   $company = $userCompany->getCompany($auth['company_id']);
?>
<script type="text/javascript" src="/js/functions/workflow.js"></script>
<div class="content_wrapper">
   <div class="padding_5">
      <div class="body_header_container">
         <?php
            // Workspace header
            echo functions::workspace_header("Workflow Chart","workflow");
         ?>
      </div>
      <div style="display:table;">
         <div style="display:table-row">
            <div style="display:table-cell"></div>
            <div class="topPointerRuler-container" style="position:relative; display:table-cell;width: auto;height: 100%;border: 1px solid #ddd;border-bottom: none;">
                  <div class="topPointerRuler-tip" style="width: 10px;background-color: #000;height: 10px;cursor: pointer;"></div>
            </div>
         </div>
         <div style="display:table-row">
            <div  class="leftPointerRuler-container" style="position:relative;display:table-cell;white-space:nowrap;vertical-align:top;width: 10px;height: 600px;border: 1px solid #ddd;">
                  <div class="leftPointerRuler-tip" style="width: 10px;background-color: #000;height: 10px;cursor: pointer;"></div>
            </div>
            <div style="display:table-cell">
              <div class="workspace workflow_ws" style="position: relative;">
                   <!-- Node -->
                   
              </div>
            </div>
         </div>
      </div>
      
      <?php
      $db = new Database();
      $workflow_id = $_GET['id'];
      $form_id = $_GET['form_id'];
      if (isset($_GET['id'])) {
         //Get orgchart information and nodes query
         $workflow_nodes_array = $db->query("SELECT * FROM tbworkflow tbw LEFT JOIN tbworkflow_objects tbwo
                                     on tbw.id = tbwo.workflow_id 
                                     WHERE tbw.id = '". $workflow_id ."'","array");
         if(count($workflow_nodes_array)==0){
            header("location:/");
         }
      
         $ws_size = json_decode($workflow_nodes_array[0]['tbw_json'],true);
         $ws_height = $ws_size['height'];
         $ws_width = $ws_size['width'];

          //node array
          $nodes_json = array();
          //extract nodes and add to array
          foreach($workflow_nodes_array as $workflow_nodes){
             array_push($nodes_json,$workflow_nodes['json']);
          }
          //convert array to json array
          $nodes_json =  json_encode($nodes_json);
          
          //Get orgchart lines query
          $workflow_lines_array = $db->query("SELECT * FROM tbworkflow_lines 
                                      WHERE workflow_id = '". $workflow_id ."'","array");
          //lines array
          $lines_json = array();
          //extract lines and add to array
          foreach($workflow_lines_array as $workflow_lines){
             array_push($lines_json,$workflow_lines['json']);
          }
          $lines_json = json_encode($lines_json);
      }
      ?>
      
      <div id="workflow_nodes" class="display"><?php echo $nodes_json; ?></div>
      <div id="workflow_lines" class="display"><?php echo $lines_json; ?></div>
      <div id="workflow_height" class="display"><?php echo $ws_height; ?></div>
      <div id="workflow_width" class="display"><?php echo $ws_width; ?></div>
      <input id="form_id" class="display" type="text" value="<?php echo $form_id ?>" />
      <input class="display userLevel" type="text" value="<?php echo $auth['user_level_id'] ?>" />
   </div>
<?php
$search = new Search();

//get departments
$getAllDepartment = $search->getAllDepartment();
echo "<div class='departments display'>" . json_encode($getAllDepartment) . "</div>";

//get positions
$getPosition = $search->getAllPosition();
echo "<div class='positions display'>" . json_encode($getPosition) . "</div>";

//get users
$getUsers = $search->getAllUsers();
echo "<div class='users display'>" . json_encode($getUsers) . "</div>";
?>